#include "terrainbrushmanipulator.h"
#include "qsignalslotcenter.h"
#include <osg\Camera>
#include "scene.h"
//#include "terrainmanager.h"
#include "environmentlayeractor.h"
#include "layersmanager.h"
#include "pickmanager.h"

namespace ControlCenter
{
	TerrainBrushManipulator::TerrainBrushManipulator(osg::Camera* maincamera):_terrainActor(NULL)
	{
		_mainCamera = maincamera;
		_usePickManager = false;
	}

	TerrainBrushManipulator::~TerrainBrushManipulator()
	{}

	void TerrainBrushManipulator::testBrush()
	{
		if(_terrainActor == NULL)
			_terrainActor = dynamic_cast<VrDataCore::EnvironmentLayerActor*>(VrDataCore::LayersManager::GetInstance(
			).getEnvironmentLayer())->getOrCreateTerrainBrush();
	}

	void TerrainBrushManipulator::mouseMove(const osgGA::GUIEventAdapter* ea0,const osgGA::GUIEventAdapter* ea1)
	{
		testBrush();
		float f1 = ea0->getX();
		float f2 = ea0->getY();
		
		//float heightz = HTSDTerrainManager::GetInstance().getTerrainHeight(_mouseCurrentpos.x(),_mouseCurrentpos.y());
		QVector<CoreBase::ActorBase*> selectobjects = VrDataCore::PickManager::GetInstance().pickObjectsNoUpdate(osg::Vec2(f1,
												f2),NODE_MASK_ALL,CLASS_DRAWABLE);
		if(selectobjects.size() == 1)
		{
			QVector<osgUtil::LineSegmentIntersector::Intersection> intersectionvec = VrDataCore::PickManager::GetInstance(
														).getPreviousLineSegmentIntersectorIntersection();
			_mouseCurrentpos = intersectionvec.at(0).getWorldIntersectPoint();
		}
		else
		{
			osg::Matrixd viewmat = getInverseMatrix();
			osg::Matrixd projmat = _mainCamera->getProjectionMatrix();
			osg::Matrixd vpmat = _mainCamera->getViewport()->computeWindowMatrix();
			osg::Matrixd mat = viewmat*projmat*vpmat;
			osg::Vec3 worldpos0 = osg::Vec3(f1,f2,0)*mat.inverse(mat);
			osg::Vec3 worldpos1 = osg::Vec3(f1,f2,1)*mat.inverse(mat);
			_mouseCurrentpos = osg::Vec3((0-worldpos1.z())*(worldpos1.x()-worldpos0.x())/(worldpos1.z()-worldpos0.z())+worldpos1.x(),
				(0-worldpos1.z())*(worldpos1.y()-worldpos0.y())/(worldpos1.z()-worldpos0.z())+worldpos1.y(),0);
			_mouseCurrentpos.z() = 0;//heightz;
		}
		_terrainActor->setTranslation(_mouseCurrentpos);
	}

	void TerrainBrushManipulator::mouseClick(const osgGA::GUIEventAdapter* ea0,const osgGA::GUIEventAdapter* ea1,
			const osgGA::GUIEventAdapter* key0,const osgGA::GUIEventAdapter* key1)
	{
		testBrush();
		switch(ea0->getButtonMask())
		{
		case osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON:
			QSignalSlotCenter::QSignalSlotCenter::GetInstance().emitDoBrush();
			break;
		default:
			break;
		}
	}

	void TerrainBrushManipulator::mouseRelease(const osgGA::GUIEventAdapter* ea0,const osgGA::GUIEventAdapter* ea1,
			const osgGA::GUIEventAdapter* key0,const osgGA::GUIEventAdapter* key1)
	{}

	void TerrainBrushManipulator::computePositionByScreenPoint(osg::Vec3& eye,osg::Vec3& center,osg::Vec3& up)
	{
		float dis;
		getLookAt(eye,center,up,dis);
		osg::Vec3 interpolation = _mouseCurrentpos - eye;
		float al = interpolation.x()/sqrt(interpolation.x()*interpolation.x()+interpolation.y()*interpolation.y());
		std::vector<osg::Vec3> newdata;
		eye = _mouseCurrentpos;
		center = _mouseCurrentpos + osg::Vec3(2000.0f*al,2000.0f*sqrt(1-al*al),0.0f);
		up = osg::Vec3(0.0f,0.0f,1.0f);
	}
}